import 'dart:io';

import 'package:bwt_ad_flutter/ad/loader/BwtServiceLoader.dart';
import 'package:bwt_ad_flutter/ad/widget/serviceWidget/BwtServiceAdWidget.dart';
import 'package:bwt_ad_flutter_example/util/Log.dart';
import 'package:bwt_ad_flutter_example/widget/SimpleWidget.dart';
import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/simple/list_notifier.dart';

class ServiceAdPage extends StatefulWidget {
  const ServiceAdPage({Key? key}) : super(key: key);

  @override
  State<ServiceAdPage> createState() => _ServiceAdPageState();
}

class _ServiceAdPageState extends State<ServiceAdPage> {
  final BwtServiceLoader _serviceLoader = BwtServiceLoader();

  BwtServiceAdWidget? _serviceView;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _serviceLoader.onAdLoad = (bwtAd) {
      setState(() {
        Log.d("serviceAd加载成功");
        _serviceView = bwtAd.serviceAdview;
      });
    };
    _serviceLoader.onAdClick = (bwtAd) {
      Log.d("serviceAd广告被点击");
    };

    _serviceLoader.onAdFailed = (msg) {
      Log.d("serviceAd加载失败了吗$msg");
    };
    _serviceLoader.onAdExpose = (bwtAd) {
      Log.d("serviceAd广告展示成功");
    };
    _serviceLoader.onAdClose = (bwtAd) {
      Log.d("serviceAd广告关闭");
    };
  }

  @override
  void dispose() {
    super.dispose();
    _serviceLoader.release();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("服务类型广告")),
      body: Center(
          child: Column(
        children: [
          SimpleWidget(
            margin: const EdgeInsets.only(top: 200),
            alignment: Alignment.center,
            width: 150,
            height: 48,
            decoration: BoxDecoration(
              color: Colors.blue,
              borderRadius: BorderRadius.circular(4),
            ),
            child: const Text(
              '加载服务类型广告',
              style: TextStyle(
                color: Colors.white,
                fontSize: 16,
              ),
            ),
            onTap: () {
              _loadAd();
            },
          ),
          Visibility(
              key: ValueKey(_serviceView?.hashCode),
              child: Container(
                margin: const EdgeInsets.only(top: 50),
                child: _serviceView,
              )),
        ],
      )),
    );
  }

  _loadAd() {
    _serviceLoader.loadAd(
        posId: Platform.isAndroid ? "32695546" : "86888360",
        adWidth: 100,
        adHeight: 100,
        showClose: true);
  }
}
